package com.util;


import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Locale;

/**
 * Created by fei on 2017/6/20.
 */

public class DownUtil2 extends Thread {
    String urlStr;
    String targetFileAbsPath;//文件名字
    long length;//文件名字
    private List<String> urllist;
    private int downitem = 0;
    private Context context;

    public DownUtil2(String urlStr, String targetFileAbsPath, long length, Context context) {
        this.urlStr = urlStr;
        this.targetFileAbsPath = targetFileAbsPath;
        this.length = length;
        this.context = context;
    }
    public DownUtil2(String urlStr, String targetFileAbsPath, long length) {
        this.urlStr = urlStr;
        this.targetFileAbsPath = targetFileAbsPath;
        this.length = length;
    }
    public DownUtil2(List<String> urllist, Context context){
        this.urllist = urllist;
        this.context = context;
        getUrl();
    }

    @Override
    public void run() {
        super.run();
        int count;

        String path = "/storage/emulated/0/test_music/";

        File file = new File(path);//路径
        if (!file.exists()) {
            MyLog.e("创建文件夹", "创建文件夹");
            file.mkdir();
        }

        File targetFile = new File(path + targetFileAbsPath);
        try {
            boolean exists = targetFile.exists();
            boolean n = targetFile.createNewFile();
        } catch (IOException e) {
        }
        try {
            URL url = new URL(urlStr);
            URLConnection connection = url.openConnection();
            connection.connect();
            int contentLength = connection.getContentLength();
            long l = contentLength - length;
            MyLog.e("王冰寒下载", "length=" + length + "  contentLength=" + contentLength + "   l=" + l + "   targetFileAbsPath" + targetFileAbsPath);
            l = (l < 0) ? -l : l;
//            sX1 = (sX1 < 0) ? -sX1 : sX1;
            if (l < 50) {
            } else {
                targetFile.delete();
                MyLog.e("删除", "删除");

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(path + targetFileAbsPath);

                byte[] buffer = new byte[1024];
                long total = 0;
                while ((count = input.read(buffer)) != -1) {
                    total += count;
                    Log.d("TAG"+contentLength, String.format(Locale.CHINA, "Download progress: %.2f%%", 100 * (total / (double) contentLength)));
                    output.write(buffer, 0, count);
                }
                output.flush();
                output.close();
                input.close();
                MyLog.e("下载完成","音频下载完成");
                downitem++;
                getUrl();
            }


        } catch (Exception e) {
            Log.e("TAG", "run: ", e);
        }
    }


    public void getUrl(){
        MyLog.e("开始下一个下载","开始下一个下载");
        SharedPreferences sharedPreferences = context.getSharedPreferences("userinfo", Context.MODE_MULTI_PROCESS);
        String videoname =  sharedPreferences.getString("videoname","");

        if (videoname == null || videoname.equals("")) {
            videoname = "0";
        }
        int namev = Integer.parseInt(videoname);

        int size = urllist.size();
        if (downitem < size){
            String downurl = urllist.get(downitem);
            String userInfo = sharedPreferences.getString(downurl,"");
            File file = new File("/storage/emulated/0/test_music/", userInfo + ".mp3");
            if (file.exists()) {
                //路径存在
                long length = file.length();
                namev++;
                new DownUtil2(downurl, "v" + namev + ".mp3",length).start();
                sharedPreferences.edit().putString(downurl,"v" + namev).commit();

            }else {
                //路径不存在
                namev++;
                new DownUtil2(downurl, "v" + namev + ".mp3",0).start();
                sharedPreferences.edit().putString(downurl,"v" + namev).commit();
            }
        }

        sharedPreferences.edit().putString("videoname","" + namev).commit();
    }


    public void downLoadUrl(){
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();
    }

}